package com.yanqu.road.server.gameplayer.module.union;

import com.yanqu.road.logic.bussiness.union.UnionWarBussiness;
import com.yanqu.road.logic.union.UnionWarManger;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.UserUnionWarMark;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

public class UnionWarModule extends GeneralModule {

    private UserUnionWarMark userUnionWarMark;

    public UnionWarModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        userUnionWarMark = UnionWarBussiness.getUserUnionWarMark(player.getUserId());
        initUserUnionWarMark();
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (null != userUnionWarMark) {
            if (userUnionWarMark.isInsertOption()) {
                UnionWarBussiness.addUserUnionWarMark(userUnionWarMark);
            } else if (userUnionWarMark.isUpdateOption()) {
                UnionWarBussiness.updateUserUnionWarMark(userUnionWarMark);
            }
        }
        return true;
    }


    public void markWatchBattle() {
        if(null != userUnionWarMark) {
            userUnionWarMark.setWatchBattleMark(UnionWarManger.getNowMark());
        }
    }

    private void initUserUnionWarMark(){
        if(null == userUnionWarMark){
            UserUnionWarMark unionWarMark = new UserUnionWarMark();
            unionWarMark.setUserId(player.getUserId());
            unionWarMark.setRankRewardMark(0);
            unionWarMark.setUnionRewardMark(0);
            unionWarMark.setWatchBattleMark(0);
            unionWarMark.setInsertOption();
            userUnionWarMark = unionWarMark;
        }
    }

    public UserUnionWarMark getUserUnionWarMark() {
        return userUnionWarMark;
    }

    /**
     * 领取家族战奖励
     * @param type
     */
    public int getUnionWarReward(int type, int position) {
        int mark = UnionWarManger.getNowMark();
        if (mark == 0) {
            return GameErrorCode.E_UNION_WAR_MARK_ERROR;
        }
        //1排行奖励 3个人奖励  2家族奖励
        switch (type) {
            case 1:
                if (userUnionWarMark.getRankRewardMark() == mark)  {
                    return GameErrorCode.E_UNION_WAR_HAS_GET_REWARD;
                }
                break;
            case 3:
                if (userUnionWarMark.getUnionRewardMark() == mark)  {
                    return GameErrorCode.E_UNION_WAR_HAS_GET_REWARD;
                }
                break;
            case 2:
                if (position == eUnionPosition.Master.getValue() || position == eUnionPosition.DeputyMaster.getValue()) {
                    UnionInfo unionInfo = UnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
                    if(null != unionInfo && unionInfo.getUnionFightRewardMark() == mark) {
                        return GameErrorCode.E_UNION_WAR_HAS_GET_REWARD;
                    }
                } else {
                    return GameErrorCode.E_UNION_WAR_GET_REWARD_NO_MASTER;
                }
                break;
            default:
                return GameErrorCode.E_UNION_WAR_REWARD_TYPE_ERROR;
        }

        return 0;
    }
}
